package April._23.answer.work6;

public class Main {
    public static void main(String[] args) {
        // 启动3个线程
        new Thread(new PrintRunnable(1),"a").start();
        new Thread(new PrintRunnable(2),"b").start();
        new Thread(new PrintRunnable(3),"c").start();
    }
}


class PrintRunnable implements Runnable{
    static int printNum = 1;
    int threadId;

    public PrintRunnable(int threadId) {
        this.threadId = threadId;
    }

    @Override
    public void run() {
        while (printNum < 75) {
            // 同步代码块
            synchronized (PrintRunnable.class) {
                // 核心就是让这个值等于线程id
                if ((printNum / 5) % 3 + 1 == threadId) {
                    // 打印5个数
                    for (int i = 0; i < 5; i++) {
                        System.out.println(Thread.currentThread().getName()+
                                "线程: " + printNum);
                        printNum++;
                    }
                    System.out.println("----------");
                    // notifyAll
                    PrintRunnable.class.notifyAll();
                }else{
                    // wait
                    try {
                        PrintRunnable.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}